<%--
  Created by IntelliJ IDEA.
  User: LC
  Date: 2019/1/20
  Time: 16:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加权限</title>
    <link rel="stylesheet" href="../static/layui/css/layui.css" media="all">
</head>
<style>
    .layui-form-select dl { max-height:160px; }
</style>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item" style="width: 350px;margin-top: 10px">
        <label class="layui-form-label">权限名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" required  lay-verify="required" placeholder="权限名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="width: 350px">
        <label class="layui-form-label">资源路径</label>
        <div class="layui-input-block">
            <input type="text" name="resource" required  lay-verify="required" placeholder="资源路径" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="width: 350px">
        <label class="layui-form-label">标识</label>
        <div class="layui-input-block">
            <input type="text" name="sn" required  lay-verify="required" placeholder="唯一标识" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="width: 350px">
        <label class="layui-form-label">父级权限组</label>
        <div class="layui-input-block">
            <select name="pId" id="parentId">
                <option value="">---请选择---</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="form">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="../static/layui/layui.js"></script>
<script>
    //Demo
    layui.use(['form', 'jquery','layer'], function(){
        var form = layui.form,
            $ = layui.jquery, //jquery;
            layer = layui.layer
        $.ajax({
            url: '/api/per/parent',
            dataType: 'json',
            type: 'post',
            success: function (data) {
                $.each(data.data, function (index, item) {
                    $('#parentId').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                })
                form.render();//下拉菜单渲染 把内容加载进去
            }
        });
        //监听提交
        form.on('submit(form)', function(data){
            $.ajax({
                url:'/api/per/addPer',
                type:'post',
                dataType : "JSON",
                contentType:'application/json',
                data:JSON.stringify(data.field),
                success:function (res) {
                    var index = parent.layer.getFrameIndex(window.name);
                    if(res.code == 200){
                        parent.layer.msg('添加成功', {icon: 1});
                        parent.layer.close(index);
                    }else{
                        parent.layer.msg('添加失败', {icon: 2});
                    }
                }
            })
            return false;
        });

        function addOptions() {
            $.ajax({
                url: '/api/per/parent',
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    $.each(data.data, function (index, item) {
                        $('#pname').append(new Option(item.name, item.pId));// 下拉菜单里添加元素
                    })
                    form.render();//下拉菜单渲染 把内容加载进去
                }
            });
        }
    });
</script>
</body>
</html>
